package vip.meeet.proxy;


import java.lang.reflect.Method;
import java.util.Arrays;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor {
    public Object creatProxyedObj(Class<?> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("xxx");
        System.out.println(Arrays.toString(objects));
        System.out.println(method.getName());
        return methodProxy.invokeSuper(o, objects);
    }

    public void hello(String name){
        System.out.println("hello " + name);
    }

    public static void main(String[] args) {
        CglibProxy cp = (CglibProxy) new CglibProxy().creatProxyedObj(CglibProxy.class);
        cp.hello("tim");
    }
}
